package tecpointbenchmark.model.modules;

import tecpointbenchmark.model.dto.ResultDTO;
import java.util.Date;

/**
 * RAM module
 * 
 * @author Rafel & Shivan
 */
public class RAMModule extends BenchmarkModule {

	long score=0;
	
    public RAMModule(ResultDTO r) {
        super(r);
        super.setModuleName("RAM");
        super.setShortDescription("Performs a benchmark test on the RAM");
        super.setLongDescription("Performs a benchmark test on the RAM by adding "
                + "values in a matrix with a certain time and then measuring the result");
    }

    /*
     * Runs the benchmark
     */
    @Override
    public void runBenchmark() {
    	/*
        // Creates a 10*3 matrix
        int[][] m = new int[10][3];*/

        // Fetch the result object
        ResultDTO result = super.getResult();

        // Allow benchmarking
        super.setContinueBenchmarking(true);

        // Current time
        result.setTime(new Date().getTime());

        // Set the result to ERROR(2)
        result.setNumber(2);
/*
        // The boolean variable 'continueBenchmarking' will remain true until
        // the handler changes it's value.
        while (super.getContinueBenchmarking()) {
            try {
                m[0][0] += 1;
                m[4][1] += 1;
                m[9][2] += 1;
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        if (tecpointbenchmark.startup.Main.debug) {
            System.out.println(result.getMessage());
            System.out.println("Module closing.");
        }
        int sum = m[0][0] + m[4][1] + m[9][2];      //The total sum*/
    	RAMthread rt = new RAMthread(this);
    	Thread t1 = new Thread(rt);
    	t1.start();

        long start = System.currentTimeMillis();

        while (super.getContinueBenchmarking()) {
            //sleep so this thread wont take all the cpu's resources
            try {
                Thread.sleep(10);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

        long end = System.currentTimeMillis();
        float time = (end - start) / 1000f;

        rt.setContinueBenchmarking(false);
  
        result.setMessage("Matrix calculations completed: " + score);
        result.setPoints((int)(score/100000000*44*(5 / time)));
    }
    
    public void setScore(long score){
    	this.score=score;
    }
}
